package com.nbsaas.boot.agent.rest.resource;

import com.nbsaas.boot.agent.api.apis.AgentCatalogApi;
import com.nbsaas.boot.entity.agent.AgentCatalog;
import com.nbsaas.boot.agent.api.domain.request.AgentCatalogRequest;
import com.nbsaas.boot.agent.api.domain.response.AgentCatalogResponse;
import com.nbsaas.boot.agent.api.domain.simple.AgentCatalogSimple;
import com.nbsaas.boot.agent.rest.convert.AgentCatalogSimpleConvert;
import com.nbsaas.boot.agent.rest.convert.AgentCatalogEntityConvert;
import com.nbsaas.boot.agent.rest.convert.AgentCatalogResponseConvert;
import com.nbsaas.boot.agent.data.repository.AgentCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.agent.api.domain.request.AgentCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   -业务接口实现
*/
@Transactional
@Service
public class AgentCatalogResource extends BaseResource<AgentCatalog,AgentCatalogResponse, AgentCatalogSimple, AgentCatalogRequest>  implements AgentCatalogApi {

    @Resource
    private AgentCatalogRepository agentCatalogRepository;

    @Override
    public JpaRepositoryImplementation<AgentCatalog, Serializable> getJpaRepository() {
        return agentCatalogRepository;
    }

    @Override
    public Function<AgentCatalog, AgentCatalogSimple> getConvertSimple() {
        return new AgentCatalogSimpleConvert();
    }

    @Override
    public Function<AgentCatalogRequest, AgentCatalog> getConvertForm() {
        return new AgentCatalogEntityConvert();
    }

    @Override
    public Function<AgentCatalog, AgentCatalogResponse> getConvertResponse() {
        return new AgentCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<AgentCatalogSimple> list(PageRequest request) {
        AgentCatalogSimpleConvert convert=new AgentCatalogSimpleConvert();
        if (request instanceof AgentCatalogSearch){
             AgentCatalogSearch searchRequest=(AgentCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<AgentCatalogSimple> root(AgentCatalogSearch search) {
        ListResponse<AgentCatalogSimple> result=new  ListResponse<>();
        List<AgentCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


